//
//  UserController.m
//  fastwatch
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "UserController.h"
#import "LoginViewController.h"
#import <BmobSDK/Bmob.h>

@interface UserController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *cacheLabel;


@end

@implementation UserController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    //设置title
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    [titleLabel setText:@"我的"];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    [titleLabel setTextColor:[UIColor blackColor]];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:19]];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    self.navigationItem.titleView = titleLabel;
    self.loginButton.layer.cornerRadius = 10;    
    [BmobUser logout];
    
}


- (IBAction)LoginBtnClicked {

        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *loginNC = [[UINavigationController alloc] initWithRootViewController:loginVC];
    loginNC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:loginNC animated:YES completion:nil];
     

    
}



- (IBAction)logoutBtnClicked:(id)sender {
 
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"注销当前用户" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alertView.tag = 100;
        [_loginButton
         addSubview:alertView];
        [alertView show];
  
   
    
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    // the user clicked OK
    
    
    if (!alertView) {
        if (buttonIndex == 0) {
            ;
        } else {
            NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *floderPath = [cachesPath stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
            
            float folderSize = [self folderSizeAtPath:floderPath];
            self.cacheLabel.text=[NSString stringWithFormat:@" %0.2f MB",folderSize];
            
        }
    }else{
    if (buttonIndex == 0)
    {
        ;
    }else{
        self.logoutBtn.hidden = YES;
        self.firstView.hidden = YES;
        self.loginButton.hidden = NO;
        self.firstLabel.hidden = NO;
        
    }
    }
}
- (IBAction)cacheBtnClicked:(id)sender {
    
       UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确认清除所有本地缓存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    alertView1.tag = 101;
    [self.view addSubview:alertView1];
    [alertView1 show];
      
    
}




//用于删除缓存的时，计算缓存大小
//单个文件的大小
- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
//遍历文件夹获得文件夹大小，返回多少M
- (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}



@end
